package Chapter_1_Fundamentals;

/**
 * Created by DB_BOY on 2018/10/23.
 */
public class BinarySearch {
    public static int rank(int key, int[] a) {

        int low = 0;
        int high = a.length - 1;
        int mid;
        while (low <= high) {
            mid = low + (high - low) / 2;
            if (a[mid] > key) {
                high = --mid;
            } else if (a[mid] < key) {
                low = ++mid;
            } else {
                return mid;
            }
        }
        return -1;
    }

    public static void main(String[] args) {

        System.out.println(System.getProperty("user.dir"));
//
        int[] a = {1, 3, 5, 7, 8, 12, 33, 45, 678};
        System.out.println(rank(459, a));

//        int[] whitelist = In.readInts(args[0]);
//        Arrays.sort(whitelist);
//
//        while (!StdIn.isEmpty()){
//            int key = StdIn.readInt();
//            if(rank(key,whitelist)<0){
//                StdOut.println(key);
//            }
//        }

    }
}
